package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findUser")
    public ModelAndView findUser(ModelAndView modelAndView) {
        List<User> allUser = userService.findAllUser ();
        modelAndView.addObject ( "allUser", allUser );
        modelAndView.setViewName ( "patients-list" );
        return modelAndView;

    }
    @RequestMapping("/findByName")
    @ResponseBody
    public List<User> findByName(String name) {
        //换一种写法
       /* String name1="%"+name+"%";
        System.out.println (name1);*/
        List<User> allUser = userService.findUserByName ( name );
        return allUser;

    }

   /* @RequestMapping(value = "/saveUser",produces = "text/html;charset=utf-8")
    //@ResponseBody
    public String saveUser(String name, String gender, Integer did) {
        System.out.println ( 123 );
        System.out.println ( gender );
        System.out.println ( did );
        userService.saveUser ( name, gender, did );
        System.out.println ( did );
        return "subscribe-form";
    }*/
   @RequestMapping(value = "/saveUser",produces = "text/html;charset=utf-8")
   //@ResponseBody
   public String saveUser(User user) {
       userService.saveUser (user);
       return "subscribe-form";
   }
}
